﻿using System;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using sl.WeiBee;

namespace TestProject1
{
    [TestClass]
    public class OAuthTest
    {
        [TestMethod]
        public void TestComputeHash()
        {
            var oauth = new FakeOAuth();
            var hmacsha1 = new HMACSHA1();
            string querystring =
                "GET&http%3A%2F%2Fapi.t.sina.com.cn%2Foauth%2Frequest_token&oauth_consumer_key%3D1758087936%26oauth_nonce%3D2050878%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1311988092%26oauth_version%3D1.0";
            hmacsha1.Key = Encoding.UTF8.GetBytes("bed16662269e81094dff3bfbe70af158&");
            var result = oauth.ComputeHash(hmacsha1, querystring);
            Assert.IsTrue(string.Compare(result, "rDdg6d6hjX2OYQlkrhClmWxjJyI=") == 0);
        }

        [TestMethod]
        public void TestParseQueryString()
        {
            string querystring = "oauth_token=50e1d6d1a66609226dbb414641660d27&oauth_token_secret=af2274b97410ecbf02e811a8ae723e21";
            var oauth = new FakeOAuth();
            oauth.GetTokenAndTokenSecret(querystring);
            Assert.IsTrue(oauth.Token == "50e1d6d1a66609226dbb414641660d27");
            Assert.IsTrue(oauth.TokenSecret == "af2274b97410ecbf02e811a8ae723e21");
        }

        [TestMethod]
        public void TestWebRequestAsync()
        {
            const string url = @"http://api.t.sina.com.cn/account/verify_credentials.xml?oauth_consumer_key=3587646579&oauth_nonce=3636566&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1312015003&oauth_token=1c1b8e4800c0cc920df3cce7e24b3e66&oauth_version=1.0&oauth_signature=8Krw5M3Q8Pj75QoZ%2fx3%2f7D49Dl4%3d";
            ////var result = Utility.WebRequestUseAsyncEnumerator(Method.GET, "http://t.sina.com.cn/", string.Empty);
            //var result2 = Utility.UrlEncode(url);
            //Assert.IsTrue(result2.Length > 2);
            //Method m = Method.GET;
            //var webRequest = WebRequest.Create(url) as HttpWebRequest;
            //Assert.IsNotNull(webRequest);
            //Assert.IsTrue(m.ToString().Length > 1);
            var oauth = new FakeOAuth();
            oauth.TestWebRequest();
        }
    }
}
